package com.itcast.googleplay;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

import com.itcast.googleplay.http.CZApi;
import com.itcast.googleplay.ui.HackyViewPager;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class PicActivity extends Activity {
	private static final String TAG = "PicActivity";
	private ArrayList<String> imgUrlList;
	private DisplayImageOptions options;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_pic);
		
		imgUrlList = getIntent().getStringArrayListExtra("imageList");
		Log.i(TAG, "获取图片的张数 = "+imgUrlList.size());
		
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_default)//在加载图片的时候,展示的默认图片
			
			.showImageForEmptyUri(R.drawable.ic_default)//uri传递出现问题时展示的图片
			
			.showImageOnFail(R.drawable.ic_default)//加载网络失败的时候,展示的图片
			
			.cacheInMemory(true)//是否需要内存缓存
			
			.cacheOnDisk(true)//是否需要磁盘缓存
			
			.considerExifParams(true)//是否支持图片自动调整,让其正常图像显示
	
			.build();//设置圆角半径
		
		HackyViewPager viewPager = (HackyViewPager) findViewById(R.id.viewpager);
		MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
		viewPager.setAdapter(myPagerAdapter);
	}
	
	class MyPagerAdapter extends PagerAdapter{

		@Override
		public int getCount() {
			return imgUrlList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
//			ImageView imageView = new ImageView(UIUtils.getContext());
			PhotoView photoView = new PhotoView(UIUtils.getContext());
			photoView.setScaleType(ScaleType.FIT_XY);
			//给imageView设置图片,网络下载,ImageLoader
			ImageLoader.getInstance().displayImage(CZApi.BASEIMGURL+imgUrlList.get(position), photoView,options);
			container.addView(photoView);
			return photoView;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}
	}
}
